package com.example.camerafacedetect.faceframe2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.media.FaceDetector;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.google.mlkit.vision.face.Face;

import java.util.List;

/**
 * Created by yubo on 2015/9/5.
 * 画人脸区域的View
 */
public class FaceView extends androidx.appcompat.widget.AppCompatImageView {

    private List<com.google.mlkit.vision.face.Face> faces;
    private Paint paint;
    private Bitmap bitmap;

    private float left;
    private float top;
    private float right;
    private float bottom;

    private int x;
    private int y;
    private int width;

    public FaceView(Context context) {
        super(context);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);//设置话出的是空心方框而不是实心方块
    }
    List<Face> mFaces;
    public void setFaces(List<Face> faces, Bitmap bitmap) {
        if(faces != null && faces.size() > 0) {
//            bitmap = ImageUtils.rotateBitmap(bitmap, 0);
            Log.e("yubo", "FaceView setFaces, face size = " + faces.size());
            this.faces = faces;
            this.bitmap = bitmap;
            setImageBitmap(bitmap);
            mFaces = faces;

            // 将会触发onDraw事件
            invalidate();
        }else{
            Log.e("yubo", "FaceView setFaces, faces == null");
        }
    }

    private float scaleRate = 1.0f;

    public void setScaleRate(float rate) {
        this.scaleRate = rate;
    }

    /** 清除数据 */
    public void clear(){
        this.faces = null;
        postInvalidate();
    }

    /** 获取人脸区域，适当扩大了一点人脸区域 */
    public Bitmap getFaceArea(){
        if(this.bitmap != null) {
            int bmWidth = bitmap.getWidth();
            int bmHeight = bitmap.getHeight();
            int delta = 50;
            width += 50;
            int height = width;
            x = (int) (left - delta);
            y = (int) (top - delta);
            if(x < 0) {
                x = 0;
            }
            if(y < 0) {
                y = 0;
            }
            if(width > bmWidth) {
                width = bmWidth;
            }
            if(height > bmHeight) {
                height = bmHeight;
            }
            return Bitmap.createBitmap(bitmap, x, y, width, height);
        }
        return null;
    }

    private void drawFaceFrame(FaceDetector.Face[] mFace, Canvas mCanvas){
        Paint mPaint = new Paint();
        //画笔颜色
        mPaint.setColor(Color.GREEN);
        //画笔的样式是矩形框，不是矩形块
        mPaint.setStyle(Paint.Style.STROKE);
        //线宽
        mPaint.setStrokeWidth(2.0f);

        for(int i=0; i < mFace.length; i++){
            PointF eyeMidPoint = new PointF();
            //两眼的中点距离
            mFace[i].getMidPoint(eyeMidPoint);
            //两眼之间的距离
            float eyesDistance = mFace[i].eyesDistance();

//            mCanvas.drawBitmap(myBitmap, 0f, 0f, null);
            //画矩形框
            mCanvas.drawRect((int)(eyeMidPoint.x-eyesDistance),
                    (int)(eyeMidPoint.y-eyesDistance),
                    (int)(eyeMidPoint.x+eyesDistance),
                    (int)(eyeMidPoint.y+eyesDistance),
                    mPaint);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(this.faces == null || faces.size() == 0) {
            return ;
        }
        Face face = faces.get(0);
        Rect rect = face.getBoundingBox();

//        drawFaceFrame(mFaces, canvas);
        canvas.drawRect(rect.left, rect.top, rect.right, rect.bottom, paint);
    }
}